package com.example.demo.project.dao;

import com.example.demo.project.entity.QQNews;
import com.example.demo.project.entity.QQUser;
import com.example.demo.project.entity.qqto.OneNews;
import com.example.demo.project.entity.qqto.OneUser;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QQUserDao{
    @Autowired
    public  MongoTemplate mongoTemplate;

    public  QQUser findOneQQUser(String username){
        Query query = new Query(Criteria.where("username").is(username));
        return mongoTemplate.findOne(query, QQUser.class);
    }
    public QQNews findOneQQNews(String newsId){
        Query query = new Query(Criteria.where("newsId").is(newsId));
        return mongoTemplate.findOne(query, QQNews.class);
    }
    //插入消息
    public boolean newsInsert(String username,String newsId,String message){
        Query query = new Query(Criteria.where("newsId").is(newsId));
        QQNews qqNews = mongoTemplate.findOne(query,QQNews.class);
        OneNews oneNews = new OneNews(username,message);
        if(qqNews.getNews()!=null){
            qqNews.getNews().add(oneNews);
            Update update= new Update().set("news", qqNews.getNews());
            UpdateResult result =mongoTemplate.updateFirst(query,update, QQNews.class);
        }
        return  true;
    }
    public boolean applyFriend (String username1,String username2,boolean flag){
        if(flag){
            //申请好友
            QQUser qqUser = findOneQQUser(username2);
            if(username1.equals(username2)){
                return false;
            }
            if(qqUser.getNoUserList().contains(username1)){
                return false;
            }
            List<OneUser> oneUsersList=qqUser.getUserList();
            for(OneUser oneUser:oneUsersList){
                if(oneUser.getUsername().equals(username1)){
                    return false;
                }
            }
            qqUser.getNoUserList().add(username1);
            Query query = new Query(Criteria.where("username").is(username2));
            Update update= new Update().set("noUserList", qqUser.getNoUserList());
            UpdateResult result =mongoTemplate.updateFirst(query,update, QQUser.class);
        }else{
            //申请失败
            QQUser qqUser = findOneQQUser(username1);
            qqUser.getNoUserList().remove(username2);
            Query query = new Query(Criteria.where("username").is(username1));
            Update update= new Update().set("noUserList", qqUser.getNoUserList());
            UpdateResult result =mongoTemplate.updateFirst(query,update, QQUser.class);
        }
        return true;
    }
}
